﻿using System.Collections.Generic;
using System.Linq;

namespace Sklep.Domain.Entities
{
    public class Cart
    {
        private readonly List<CartLine> _lineCollection = new List<CartLine>();
 
        public void AddItem(Product product, int quantity)
        {
            CartLine line = _lineCollection.FirstOrDefault(x => x.Product.ProductID == product.ProductID);

            if (line == null)
                _lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
            else
                line.Quantity += quantity;
        }

        public void RemoveLine(Product product)
        {
            _lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
        }

        public decimal ComputeTotalValue()
        {
            return _lineCollection.Sum(x => x.Product.Price*x.Quantity);
        }

        public void Clear()
        {
            _lineCollection.Clear();
        }

        public IEnumerable<CartLine> Lines {get { return _lineCollection; }}
 
    }
}
